package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * WebMvc配置
 *
 * @author Dui
 * @since 2022/10/31 19:30
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**\
     * 资源映射
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //当浏览器访问：/backend/**   ---> /backend/
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        //当浏览器访问：/front/**   ---> /front/
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
    }
    /**
     * 消息转换
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //使用自定义的消息转换器
        converter.setObjectMapper(new JacksonObjectMapper());
        //加到第一位，优先使用我们自定义的这种消息转换器
        converters.add(0,converter);
    }
}